package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.ApiResult;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.serviceimpl.TeacherServiceImpl;
import com.rabbiter.oes.util.ApiResultHandler;
import com.rabbiter.oes.util.RedisUtils;
import com.rabbiter.oes.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

@RestController
public class TeacherController {

    @Autowired
    private RedisUtils redisUtils;

    private TeacherServiceImpl teacherService;

    @Autowired
    public TeacherController(TeacherServiceImpl teacherService) {
        this.teacherService = teacherService;
    }
    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size) {
        Page<Teacher> teacherPage = new Page<>(page, size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);

        return ApiResultHandler.buildApiResult(200, "查询所有教师", teacherIPage);
    }

    @GetMapping("/teacher/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId) {
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }

    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId) {
        return ApiResultHandler.success(teacherService.deleteById(teacherId));
    }

    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher) {
        return ApiResultHandler.success(teacherService.update(teacher));
    }

    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher) {
        return ApiResultHandler.success(teacherService.add(teacher));
    }

    @PostMapping("/send")
    public ApiResult send(String phone) {
        String reg = "^((13[0-9])|(14[0|5|6|7|9])|(15[0|1|2|3|5|6|7|8|9])|(16[2|5|6|7])|(17[0|1|2|3|5|6|7|8])|(18[0-9])|(19[0|1|2|3|5|6|7|8|9]))\\\\d{8}$";
        boolean isValid = Pattern.matches(reg, phone);
        if (isValid) {
            return ApiResultHandler.success("该手机号不规范");
        }

        Teacher teacher = teacherService.getByPhone(phone);
        if (teacher == null) {
            return ApiResultHandler.success("该手机号未注册");
        }
        String code = SMSUtil.getCode();
        SMSUtil.sendSMS(phone,code);
        redisUtils.set(phone,code,60 * 5);
        SMSUtil.sendSMS(phone, SMSUtil.getCode());
        return ApiResultHandler.success("发送成功");
    }

    @PostMapping("/loginPhone")
    public ApiResult loginPhone(String phone, String code, HttpServletResponse response) {
        String redisCode = (String) redisUtils.get(phone);
        if (!redisCode.equals(code)) {
            return ApiResultHandler.success("验证码错误");
        }
        Teacher teacherRes = teacherService.getByPhone(phone);
        Cookie token = new Cookie("rb_token", teacherRes.getCardId());
        token.setPath("/");
        Cookie role = new Cookie("rb_role", "1");
        role.setPath("/");
        response.addCookie(token);
        response.addCookie(role);
        return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
    }
}
